require('dotenv').config() // Sets up dotenv as soon as our application starts

const express = require('express')
const logger = require('morgan')
const bodyParser = require('body-parser')

const cors = require('cors')
const router = require('./router/index.js')

const environment = process.env.NODE_ENV
const stage = require('./config')[environment]

const app = express()

app.use(cors())

// app.use(bodyParser.arguments())
app.use(bodyParser.urlencoded({
  extended: true
}))
app.use(bodyParser.json())

if (environment !== 'production') {
  app.use(logger('dev'))
}

app.use('/api/v1', router)

const server = app.listen(`${stage.port}`, function () {
  const host = server.address().address
  const port = server.address().port
  console.log('HTTP Server is running on http://%s:%s', host, port)
})

module.exports = app
